package com.goodsogood.union.auth.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.goodsogood.union.auth.exception.TokenDecryptException;
import com.goodsogood.union.auth.service.AccessToken;

public class TokenUtil {
	
	public static void main(String[] args) throws Exception {
		String s1="TokenUtil343535643f56yt677|{}";
		System.out.println(s1);
		String s2 = AESUtils.encrypt(s1, "123456789") ;
		System.out.println(s2);
		System.out.println(AESUtils.decrypt(s2, "123456789"));
	}
	
	
	public static String generate(String raw) throws Exception{
	   return AESUtils.encrypt(raw, AESUtils.KEY ) ;
	}
	
	public static AccessToken decryptToken(String token)  {
		String json;
		try {
			json = AESUtils.decrypt(token, AESUtils.KEY);
			System.out.println(json);
			return JSON.parseObject(json, new TypeReference<AccessToken>() {}) ;
		} catch (Exception e) {
			throw new TokenDecryptException(token) ; 
		}
		
	}
	

}
